package cn.exrick.xboot.common.jiuqiCommon.callAPIUtil;

import cn.exrick.xboot.common.jiuqiCommon.callAPIUtil.dao.JDBCimpl;
import cn.exrick.xboot.common.utils.SpringUtils;
import cn.exrick.xboot.modules.callApi.entity.SMS;
import cn.exrick.xboot.modules.httpMAS.config.SMSConfig;
import cn.exrick.xboot.modules.httpMAS.server.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class CallSMSservice {

    @Value("${sms.usePlatform}")
    private boolean usePlatform=true;
    /**
     * 发送短信
     *
     * @param sms
     * @return
     */
    public boolean sendSMS(SMS sms) {
        boolean isok = false;
        if(usePlatform) {
            isok = sendMaxSMS(sms);
        }else {
            isok=sendYgSMS(sms);
        }
        return isok;
    }

    /**
     * 20200703 njp
     * 移动云Max短信平台的发送短信方法
     * @param sms
     * @return
     */
    public boolean sendMaxSMS(SMS sms){
        boolean isok = false;
        int i = 0;
        try {
            SMSUtils  smsUtils=new SMSUtils();
            if(sms.getMESSAGECONTENT().length()>10){
                String[] split = sms.getMESSAGECONTENT().split(":");
                //这是根据自己的模板规则在原有的逻辑进行的修改，后续如果开发新的接口调用建议重载或重构sendSMS（）方法来进行通用逻辑的支持
                if(split.length>1) {
                    i = smsUtils.sendMsg(sms.getDESTADDR(), split[0],split[1]);
                }
            }else {
                i = smsUtils.sendMsg(sms.getDESTADDR(), sms.getMESSAGECONTENT());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (i == 1) {
            isok = true;
        } else {
            isok = false;
        }
        return isok;
    }

    /**
     * 盈光短信平台短信发送
     * @param sms
     * @return
     */
    public boolean sendYgSMS(SMS sms){
        boolean isok = false;
        JDBCimpl jDBCImpl = new JDBCimpl();
        try {
            //StringBuffer content = new StringBuffer("路桥测试短信：验证码为123456");
            //Date date = new Date();
            sms.setREQUESTTIME(CallSMSservice.getTime());
            String sql = CallSMSservice.getSql(sms);
            isok = jDBCImpl.update(sql);
            //jDBCImpl.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            return isok;
        } finally {
            jDBCImpl.close();
        }
        return isok;
    }




    public static String getUUID() {
        //注意replaceAll前面的是正则表达式
        //String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        //String uuid = UUID.randomUUID().toString();
        String orderId = UUID.randomUUID().toString().hashCode() + "";
        return orderId;
    }

    /**
     * 获取指定格式的时间格式
     *
     * @return
     */
    public static Date getTime() {
        //取当前时间
        Date nowdate = new Date();
        //转换时间格式
        SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date timestamp = Timestamp.valueOf(simpleDate.format(nowdate));
        return timestamp;
    }

    public static String getSql(SMS sms) {
        sms.setSISMSID(CallSMSservice.getUUID());
        String sql = "Insert into sms_outbox  (sismsid, extcode, destaddr, messagecontent, \n" +
                "reqdeliveryreport,msgfmt,sendmethod,requesttime,applicationid)VALUES  \n" +
                "('" + sms.getSISMSID() + "', '16', '" + sms.getDESTADDR() + "',  ' " + sms.getMESSAGECONTENT() + " ', 1, 15, 0, '" + sms.getREQUESTTIME() + "', 'smssend')";
        return sql;
    }

    public static void main(String[] args) {
        System.out.println(CallSMSservice.getUUID());
    }
}
